/*!
 * tags jQuery Library v1.0
 *
 * Copyright 2011, dimpogi
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 *
 * Date: Sat August 27 14:16:56 2011 +0800
 */

(function($){
    $.fn.extend({ 
        generateTags: function(arrOptions) {
 
            var defaults = {
                url: 'tags/',
                keywords: '',
                numberOfItems: 5,
                wrapId: 'site_tags',
                wrapClass: '',
                listClass: ''
            };
             
            var arrOptions = $.extend(defaults, arrOptions);
         
            return this.each(function() {
                  var arrObjOptions = arrOptions;
                  var obj = $(this);
                  
                  var strCreatedList = generateList();
                  
                  obj.html(strCreatedList);
                  
                  function generateList() 
                  {
                	  if (arrObjOptions.wrapClass.length > 0) {
                		  var strList = '<ul id="' + arrObjOptions.wrapId +'" class="' + arrObjOptions.wrapClass + '">';                		  
                	  } else {
                		  var strList = '<ul id="' + arrObjOptions.wrapId +'">';
                	  }
                	  
                	  if (arrObjOptions.listClass.length > 0) {
                		  var strClasses = 'class = "' + arrObjOptions.listClass + '"';
                	  }
                	  
                	  var strKeyword = arrObjOptions.keywords.replace(' ', '-');
                	  
                	  strList = strList + '<li ' + strClasses + '><a href="' + arrObjOptions.url + strKeyword + '.html">' + strKeyword + '</a></li>';

                	  var arrExtractedKeywords = arrObjOptions.keywords.split(' ');
                	  var intCounter = 1;
                	  
                	  $.each(arrExtractedKeywords, function( strIndex, strValue ){
                		  
                		  strKeyword = strValue.replace(' ', '-');
                		  
                		  if (intCounter >= arrObjOptions.numberOfItems) {
                			  return;
                		  } else {
                			  var strClasses = '';
                			  strList = strList + '<li ' + strClasses + '><a href="' + arrObjOptions.url + strKeyword + '.html">' + strKeyword + '</a></li>';
                			  
                			  intCounter++;                			  
                		  }
        			  });
                	  
                	  strList = strList + '</ul>';

                	  return strList;
                  }
            });
        }
    });
})(jQuery);